package com.snap.vseries.adapter;

import android.content.Context;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.Spinner;
import android.widget.TextView;

import com.snap.vseries.R;
import com.snap.vseries.base.BaseRecycleAdapter;
import com.snap.vseries.base.BaseViewHolder;
import com.snap.vseries.bean.ProgramRecycle;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;

public class NewExpLightingAdapter extends BaseRecycleAdapter<ProgramRecycle> {

    public NewExpLightingAdapter(Context context, List<ProgramRecycle> dataList) {
        super(context, dataList);
    }

    @Override
    public void bindData(BaseViewHolder holder, ProgramRecycle data, int position) {
        ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
        if (data.getStartIndex() == -1 || data.getEndIndex() == -1) {
            //隐藏item要把高度宽度设为0；
            holder.itemView.setVisibility(View.GONE);
            layoutParams.height = 0;
            layoutParams.width = 0;
            return;
        } else {
            //显示item要重新设置高度宽度；
            holder.itemView.setVisibility(View.VISIBLE);
            layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        }
        String stage = String.format(Locale.CHINA, "循环 %02d", position + 1);
        holder.setText(R.id.tv_stage, stage);
        ((SwitchCompat) holder.getView(R.id.btn_sw)).setChecked(data.isLighting());
        ((SwitchCompat) holder.getView(R.id.btn_sw)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                data.setLighting(isChecked);
            }
        });
        Spinner spinner = holder.getView(R.id.tv_sections);
        showSpinner(spinner, data);
    }

    private void showSpinner(Spinner spinner, ProgramRecycle data) {
        List<String> list = new ArrayList<>();
        for (int i = data.getStartIndex(); i <= data.getEndIndex(); i++) {
            list.add(String.format(Locale.CHINA, "Stage %02d", i));
        }
        ArrayAdapter<String> spinnerAdapter;
        spinnerAdapter = new ArrayAdapter<>(mContext, R.layout.spinner_layout, list);

        spinner.setAdapter(spinnerAdapter);
        for (int i = 0; i < list.size(); i++) {
            Pattern pattern = Pattern.compile(" ");
            String[] strs = pattern.split(list.get(i));
            if (data.getLightingIndex() == Integer.parseInt(strs[1])) {
                spinner.setSelection(i);
                break;
            }
        }


        //设置选中之后的监听
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                TextView tv = (TextView) view;
                if (tv == null) {
                    return;
                }
                tv.setTextColor(0xFF568BFF);
                tv.setTextSize(14);
                //设置选中的文字显示风格
                Pattern pattern = Pattern.compile(" ");
                String[] strs = pattern.split(tv.getText().toString());
                data.setLightingIndex(Integer.parseInt(strs[1]));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    @Override
    public int getItemCount() {
        return super.getItemCount();
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_experiment_lighting;
    }

    @Override
    public void updateData(List<ProgramRecycle> dataList) {
        super.updateData(dataList);
    }
}
